home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / malloc / ralloc.c < prev    next >
C/C++ Source or Header  |  1993-11-29  |  14KB  |  529 lines

  1. /* Block-relocating memory allocator. 
  2.    Copyright (C) 1993 Free Software Foundation, Inc.
  3.  
  4.  
  5. This file is part of the GNU C Library.  Its master source is NOT part of
  6. the C library, however.  The master source lives in /gd/gnu/lib.
  7.  
  8. The GNU C Library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Library General Public License as
  10. published by the Free Software Foundation; either version 2 of the
  11. License, or (at your option) any later version.
  12.  
  13. The GNU C Library is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. Library General Public License for more details.
  17.  
  18. You should have received a copy of the GNU Library General Public
  19. License along with the GNU C Library; see the file COPYING.LIB.  If
  20. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  21. Cambridge, MA 02139, USA.  */
  22.  
  23. /* NOTES:
  24.  
  25.    Only relocate the blocs necessary for SIZE in r_alloc_sbrk,
  26.    rather than all of them.  This means allowing for a possible
  27.    hole between the first bloc and the end of malloc storage. */
  28.  
  29. #ifdef emacs
  30.  
  31. #include <config.h>
  32. #include "lisp.h"        /* Needed for VALBITS.  */
  33.  
  34. #undef NULL
  35.  
  36. /* The important properties of this type are that 1) it's a pointer, and
  37.    2) arithmetic on it should work as if the size of the object pointed
  38.    to has a size of 1.  */
  39. #if 0 /* Arithmetic on void* is a GCC extension.  */
  40. #ifdef __STDC__
  41. typedef void *POINTER;
  42. #else
  43.  
  44. #ifdef    HAVE_CONFIG_H
  45. #include "config.h"
  46. #endif
  47.  
  48. typedef char *POINTER;
  49.  
  50. #endif
  51. #endif /* 0 */
  52.  
  53. /* Unconditionally use char * for this.  */
  54. typedef char *POINTER;
  55.  
  56. typedef unsigned long SIZE;
  57.  
  58. /* Declared in dispnew.c, this version doesn't screw up if regions
  59.    overlap.  */
  60. extern void safe_bcopy ();
  61.  
  62. #include "getpagesize.h"
  63.  
  64. #else    /* Not emacs.  */
  65.  
  66. #include <stddef.h>
  67.  
  68. typedef size_t SIZE;
  69. typedef void *POINTER;
  70.  
  71. #include <unistd.h>
  72. #include <malloc.h>
  73. #include <string.h>
  74.  
  75. #define safe_bcopy(x, y, z) memmove (y, x, z)
  76.  
  77. #endif    /* emacs.  */
  78.  
  79. #define NIL ((POINTER) 0)
  80.  
  81. /* A flag to indicate whether we have initialized ralloc yet.  For
  82.    Emacs's sake, please do not make this local to malloc_init; on some
  83.    machines, the dumping procedure makes all static variables
  84.    read-only.  On these machines, the word static is #defined to be
  85.    the empty string, meaning that r_alloc_initialized becomes an
  86.    automatic variable, and loses its value each time Emacs is started up.  */
  87. static int r_alloc_initialized = 0;
  88.  
  89. static void r_alloc_init ();
  90.  
  91. /* Declarations for working with the malloc, ralloc, and system breaks.  */
  92.  
  93. /* Function to set the real break value. */
  94. static POINTER (*real_morecore) ();
  95.  
  96. /* The break value, as seen by malloc (). */
  97. static POINTER virtual_break_value;
  98.  
  99. /* The break value, viewed by the relocatable blocs. */
  100. static POINTER break_value;
  101.  
  102. /* The REAL (i.e., page aligned) break value of the process. */
  103. static POINTER page_break_value;
  104.  
  105. /* This is the size of a page.  We round memory requests to this boundary.  */
  106. static int page_size;
  107.  
  108. /* Whenever we get memory from the system, get this many extra bytes.  This 
  109.    must be a multiple of page_size.  */
  110. static int extra_bytes;
  111.  
  112. /* Macros for rounding.  Note that rounding to any value is possible
  113.    by changing the definition of PAGE. */
  114. #define PAGE (getpagesize ())
  115. #define ALIGNED(addr) (((unsigned long int) (addr) & (page_size - 1)) == 0)
  116. #define ROUNDUP(size) (((unsigned long int) (size) + page_size - 1) \
  117.                & ~(page_size - 1))
  118. #define ROUND_TO_PAGE(addr) (addr & (~(page_size - 1)))
  119.  
  120. /* Functions to get and return memory from the system.  */
  121.  
  122. /* Obtain SIZE bytes of space.  If enough space is not presently available
  123.    in our process reserve, (i.e., (page_break_value - break_value)),
  124.    this means getting more page-aligned space from the system.
  125.  
  126.    Return non-zero if all went well, or zero if we couldn't allocate
  127.    the memory.  */
  128. static int
  129. obtain (size)
  130.      SIZE size;
  131. {
  132.   SIZE already_available = page_break_value - break_value;
  133.  
  134.   if (already_available < size)
  135.     {
  136.       SIZE get = ROUNDUP (size - already_available);
  137.       /* Get some extra, so we can come here less often.  */
  138.       get += extra_bytes;
  139.  
  140.       if ((*real_morecore) (get) == 0)
  141.     return 0;
  142.  
  143.       page_break_value += get;
  144.     }
  145.  
  146.   break_value += size;
  147.  
  148.   return 1;
  149. }
  150.  
  151. /* Obtain SIZE bytes of space and return a pointer to the new area.
  152.    If we could not allocate the space, return zero.  */
  153.  
  154. static POINTER
  155. get_more_space (size)
  156.      SIZE size;
  157. {
  158.   POINTER ptr = break_value;
  159.   if (obtain (size))
  160.     return ptr;
  161.   else
  162.     return 0;
  163. }
  164.  
  165. /* Note that SIZE bytes of space have been relinquished by the process.
  166.    If SIZE is more than a page, return the space to the system. */
  167.  
  168. static void
  169. relinquish (size)
  170.      SIZE size;
  171. {
  172.   POINTER new_page_break;
  173.   int excess;
  174.  
  175.   break_value -= size;
  176.   new_page_break = (POINTER) ROUNDUP (break_value);
  177.   excess = (char *) page_break_value - (char *) new_page_break;
  178.   
  179.   if (excess > extra_bytes * 2)
  180.     {
  181.       /* Keep extra_bytes worth of empty space.
  182.      And don't free anything unless we can free at least extra_bytes.  */
  183.       if ((*real_morecore) (extra_bytes - excess) == 0)
  184.     abort ();
  185.  
  186.       page_break_value += extra_bytes - excess;
  187.     }
  188.  
  189.   /* Zero the space from the end of the "official" break to the actual
  190.      break, so that bugs show up faster.  */
  191.   bzero (break_value, ((char *) page_break_value - (char *) break_value));
  192. }
  193.  
  194. /* The meat - allocating, freeing, and relocating blocs.  */
  195.  
  196. /* These structures are allocated in the malloc arena.
  197.    The linked list is kept in order of increasing '.data' members.
  198.    The data blocks abut each other; if b->next is non-nil, then
  199.    b->data + b->size == b->next->data.  */
  200. typedef struct bp
  201. {
  202.   struct bp *next;
  203.   struct bp *prev;
  204.   POINTER *variable;
  205.   POINTER data;
  206.   SIZE size;
  207. } *bloc_ptr;
  208.  
  209. #define NIL_BLOC ((bloc_ptr) 0)
  210. #define BLOC_PTR_SIZE (sizeof (struct bp))
  211.  
  212. /* Head and tail of the list of relocatable blocs. */
  213. static bloc_ptr first_bloc, last_bloc;
  214.  
  215. /* Find the bloc referenced by the address in PTR.  Returns a pointer
  216.    to that block. */
  217.  
  218. static bloc_ptr
  219. find_bloc (ptr)
  220.      POINTER *ptr;
  221. {
  222.   register bloc_ptr p = first_bloc;
  223.  
  224.   while (p != NIL_BLOC)
  225.     {
  226.       if (p->variable == ptr && p->data == *ptr)
  227.     return p;
  228.  
  229.       p = p->next;
  230.     }
  231.  
  232.   return p;
  233. }
  234.  
  235. /* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
  236.    Returns a pointer to the new bloc, or zero if we couldn't allocate
  237.    memory for the new block.  */
  238.  
  239. static bloc_ptr
  240. get_bloc (size)
  241.      SIZE size;
  242. {
  243.   register bloc_ptr new_bloc;
  244.  
  245.   if (! (new_bloc = (bloc_ptr) malloc (BLOC_PTR_SIZE))
  246.       || ! (new_bloc->data = get_more_space (size)))
  247.     {
  248.       if (new_bloc)
  249.     free (new_bloc);
  250.  
  251.       return 0;
  252.     }
  253.  
  254.   new_bloc->size = size;
  255.   new_bloc->next = NIL_BLOC;
  256.   new_bloc->variable = (POINTER *) NIL;
  257.  
  258.   if (first_bloc)
  259.     {
  260.       new_bloc->prev = last_bloc;
  261.       last_bloc->next = new_bloc;
  262.       last_bloc = new_bloc;
  263.     }
  264.   else
  265.     {
  266.       first_bloc = last_bloc = new_bloc;
  267.       new_bloc->prev = NIL_BLOC;
  268.     }
  269.  
  270.   return new_bloc;
  271. }
  272.  
  273. /* Relocate all blocs from BLOC on upward in the list to the zone
  274.    indicated by ADDRESS.  Direction of relocation is determined by
  275.    the position of ADDRESS relative to BLOC->data.
  276.  
  277.    If BLOC is NIL_BLOC, nothing is done.
  278.  
  279.    Note that ordering of blocs is not affected by this function. */
  280.  
  281. static void
  282. relocate_some_blocs (bloc, address)
  283.      bloc_ptr bloc;
  284.      POINTER address;
  285. {
  286.   if (bloc != NIL_BLOC)
  287.     {
  288.       register SIZE offset = address - bloc->data;
  289.       register SIZE data_size = 0;
  290.       register bloc_ptr b;
  291.       
  292.       for (b = bloc; b != NIL_BLOC; b = b->next)
  293.     {
  294.       data_size += b->size;
  295.       b->data += offset;
  296.       *b->variable = b->data;
  297.     }
  298.  
  299.       safe_bcopy (address - offset, address, data_size);
  300.     }
  301. }
  302.  
  303.  
  304. /* Free BLOC from the chain of blocs, relocating any blocs above it
  305.    and returning BLOC->size bytes to the free area. */
  306.  
  307. static void
  308. free_bloc (bloc)
  309.      bloc_ptr bloc;
  310. {
  311.   if (bloc == first_bloc && bloc == last_bloc)
  312.     {
  313.       first_bloc = last_bloc = NIL_BLOC;
  314.     }
  315.   else if (bloc == last_bloc)
  316.     {
  317.       last_bloc = bloc->prev;
  318.       last_bloc->next = NIL_BLOC;
  319.     }
  320.   else if (bloc == first_bloc)
  321.     {
  322.       first_bloc = bloc->next;
  323.       first_bloc->prev = NIL_BLOC;
  324.     }
  325.   else
  326.     {
  327.       bloc->next->prev = bloc->prev;
  328.       bloc->prev->next = bloc->next;
  329.     }
  330.  
  331.   relocate_some_blocs (bloc->next, bloc->data);
  332.   relinquish (bloc->size);
  333.   free (bloc);
  334. }
  335.  
  336. /* Interface routines.  */
  337.  
  338. static int use_relocatable_buffers;
  339.  
  340. /* Obtain SIZE bytes of storage from the free pool, or the system, as
  341.    necessary.  If relocatable blocs are in use, this means relocating
  342.    them.  This function gets plugged into the GNU malloc's __morecore
  343.    hook.
  344.  
  345.    We provide hysteresis, never relocating by less than extra_bytes.
  346.  
  347.    If we're out of memory, we should return zero, to imitate the other
  348.    __morecore hook values - in particular, __default_morecore in the
  349.    GNU malloc package.  */
  350.  
  351. POINTER 
  352. r_alloc_sbrk (size)
  353.      long size;
  354. {
  355.   /* This is the first address not currently available for the heap.  */
  356.   POINTER top;
  357.   /* Amount of empty space below that.  */
  358.   /* It is not correct to use SIZE here, because that is usually unsigned.
  359.      ptrdiff_t would be okay, but is not always available.
  360.      `long' will work in all cases, in practice.  */
  361.   long already_available;
  362.   POINTER ptr;
  363.  
  364.   if (! use_relocatable_buffers)
  365.     return (*real_morecore) (size);
  366.  
  367.   top = first_bloc ? first_bloc->data : page_break_value;
  368.   already_available = (char *) top - (char *) virtual_break_value;
  369.  
  370.   /* Do we not have enough gap already?  */
  371.   if (size > 0 && already_available < size)
  372.     {
  373.       /* Get what we need, plus some extra so we can come here less often.  */
  374.       SIZE get = size - already_available + extra_bytes;
  375.  
  376.       if (! obtain (get))
  377.     return 0;
  378.  
  379.       if (first_bloc)
  380.     relocate_some_blocs (first_bloc, first_bloc->data + get);
  381.  
  382.       /* Zero out the space we just allocated, to help catch bugs
  383.      quickly.  */
  384.       bzero (virtual_break_value, get);
  385.     }
  386.   /* Can we keep extra_bytes of gap while freeing at least extra_bytes?  */
  387.   else if (size < 0 && already_available - size > 2 * extra_bytes)
  388.     {
  389.       /* Ok, do so.  This is how many to free.  */
  390.       SIZE give_back = already_available - size - extra_bytes;
  391.  
  392.       if (first_bloc)
  393.     relocate_some_blocs (first_bloc, first_bloc->data - give_back);
  394.       relinquish (give_back);
  395.     }
  396.  
  397.   ptr = virtual_break_value;
  398.   virtual_break_value += size;
  399.  
  400.   return ptr;
  401. }
  402.  
  403. /* Allocate a relocatable bloc of storage of size SIZE.  A pointer to
  404.    the data is returned in *PTR.  PTR is thus the address of some variable
  405.    which will use the data area.
  406.  
  407.    If we can't allocate the necessary memory, set *PTR to zero, and
  408.    return zero.  */
  409.  
  410. POINTER
  411. r_alloc (ptr, size)
  412.      POINTER *ptr;
  413.      SIZE size;
  414. {
  415.   register bloc_ptr new_bloc;
  416.  
  417.   if (! r_alloc_initialized)
  418.     r_alloc_init ();
  419.  
  420.   new_bloc = get_bloc (size);
  421.   if (new_bloc)
  422.     {
  423.       new_bloc->variable = ptr;
  424.       *ptr = new_bloc->data;
  425.     }
  426.   else
  427.     *ptr = 0;
  428.  
  429.   return *ptr;
  430. }
  431.  
  432. /* Free a bloc of relocatable storage whose data is pointed to by PTR.
  433.    Store 0 in *PTR to show there's no block allocated.  */
  434.  
  435. void
  436. r_alloc_free (ptr)
  437.      register POINTER *ptr;
  438. {
  439.   register bloc_ptr dead_bloc;
  440.  
  441.   dead_bloc = find_bloc (ptr);
  442.   if (dead_bloc == NIL_BLOC)
  443.     abort ();
  444.  
  445.   free_bloc (dead_bloc);
  446.   *ptr = 0;
  447. }
  448.  
  449. /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
  450.    Do this by shifting all blocks above this one up in memory, unless
  451.    SIZE is less than or equal to the current bloc size, in which case
  452.    do nothing.
  453.  
  454.    Change *PTR to reflect the new bloc, and return this value.
  455.  
  456.    If more memory cannot be allocated, then leave *PTR unchanged, and
  457.    return zero.  */
  458.  
  459. POINTER
  460. r_re_alloc (ptr, size)
  461.      POINTER *ptr;
  462.      SIZE size;
  463. {
  464.   register bloc_ptr bloc;
  465.  
  466.   bloc = find_bloc (ptr);
  467.   if (bloc == NIL_BLOC)
  468.     abort ();
  469.  
  470.   if (size <= bloc->size)
  471.     /* Wouldn't it be useful to actually resize the bloc here?  */
  472.     return *ptr;
  473.  
  474.   if (! obtain (size - bloc->size))
  475.     return 0;
  476.  
  477.   relocate_some_blocs (bloc->next, bloc->data + size);
  478.  
  479.   /* Zero out the new space in the bloc, to help catch bugs faster.  */
  480.   bzero (bloc->data + bloc->size, size - bloc->size);
  481.  
  482.   /* Indicate that this block has a new size.  */
  483.   bloc->size = size;
  484.  
  485.   return *ptr;
  486. }
  487.  
  488. /* The hook `malloc' uses for the function which gets more space
  489.    from the system.  */
  490. extern POINTER (*__morecore) ();
  491.  
  492. /* Initialize various things for memory allocation. */
  493.  
  494. static void
  495. r_alloc_init ()
  496. {
  497.   if (r_alloc_initialized)
  498.     return;
  499.  
  500.   r_alloc_initialized = 1;
  501.   real_morecore = __morecore;
  502.   __morecore = r_alloc_sbrk;
  503.  
  504.   virtual_break_value = break_value = (*real_morecore) (0);
  505.   if (break_value == NIL)
  506.     abort ();
  507.  
  508.   page_size = PAGE;
  509.   extra_bytes = ROUNDUP (50000);
  510.  
  511.   page_break_value = (POINTER) ROUNDUP (break_value);
  512.  
  513.   /* The extra call to real_morecore guarantees that the end of the
  514.      address space is a multiple of page_size, even if page_size is
  515.      not really the page size of the system running the binary in
  516.      which page_size is stored.  This allows a binary to be built on a
  517.      system with one page size and run on a system with a smaller page
  518.      size. */
  519.   (*real_morecore) (page_break_value - break_value);
  520.  
  521.   /* Clear the rest of the last page; this memory is in our address space
  522.      even though it is after the sbrk value.  */
  523.   /* Doubly true, with the additional call that explicitly adds the
  524.      rest of that page to the address space.  */
  525.   bzero (break_value, (page_break_value - break_value));
  526.   virtual_break_value = break_value = page_break_value;
  527.   use_relocatable_buffers = 1;
  528. }
  529.